--分离器
function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}
	while true do
		local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
		if not nFindLastIndex then
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
		break
		end
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
		nFindStartIndex = nFindLastIndex + string.len(szSeparator)
		nSplitIndex = nSplitIndex + 1
	end
	return nSplitArray
end



--随机产生不可重复的数
function sel_dist_num(count,...)
	selected={};
	math.random(0,#arg);
	--math.randomseed(microtime()*1000000);
	math.randomseed(microtime()*1000000);
	if #arg<=count then return arg end
	while #selected < count do
		math.random(#arg);
		table.insert(selected,table.remove(arg,math.random(#arg)));
	end
	return selected;
end



--格式化字符串
function format_num( num )
	num = tonumber(num)
	if num < 10 then
		return "0"..num
	end
	return num;
end

--格式化倍数
function format_power( num )
	num = tonumber(num)
	if num < 10 then
		return "000"..num
	end
	if num < 100 and num >9 then
		return "00"..num
	end
	if num < 1000 and num > 99 then
		return "0"..num
	end
	return num;
end

--数字字符串排序
function sort_str( num , max)
	local out = ""
	local st = {}
	if num == nil then return nil end
	local len = string.len( num )
	if len%2 ~= 0 then
		return nil
	end

	for i=1,len,2 do
		c = string.sub(num,i,i+1)
		if tonumber(c) > max or tonumber(c) <1 then return nil end
		if not in_table( c, st ) then
			table.insert( st, c )
		else
			return nil
		end
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end


--排列组合 m>=n
function permutation( m, n )
	local out=1;
	if m==n then
		for i=1,m do
			out = out*i
		end
	elseif m<n then
		return 0
	else
		for i=m-n+1,m do
			out = out*i
		end
	end

	return out
end

function combination(m, n)
	local out = permutation(m, n)
	local out1 = 1;
	for i=1,n do
		out1 = out1*i
	end
	return (out/out1)
end

--判断字符串是否有相交
function is_str_mix( str1, str2 )
	local t1 = ""
	local len1 = string.len(str1)
	local len2 = string.len(str2)

	for i=1,len1,2 do
		t1 = string.sub(str1,i,i+1)
		for j=1,len2,2 do
			if( t1 == string.sub(str2,j,j+1) ) then return 1 end
		end
	end

	return nil
end

function random_kl10( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选  任选 1#1011#任选[1-5]*注数#倍数 1#1011#1*5#10
	if butType == '1' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		rjNum = tonumber(xItem[1])  --任选基数
		butNum = tonumber(xItem[2])
		if rjNum > 5 or rjNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if rjNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 4 then
			for i=1,butNum do
				local nums = sel_dist_num(4,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 5 then
			for i=1,butNum do
				local nums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('1', zlStr)
	--单式机选组选 1#1012#组选[2/3]*注数#倍数 1#1012#2*5#10
	elseif butType == '2' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		zjNum = tonumber(xItem[1])  --组选基数
		butNum = tonumber(xItem[2])
		if zjNum > 3 or zjNum < 2 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		--二连组
		if zjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		--三前组
		elseif zjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('2', zlStr)

	--单式机选直选 1#1013#直选[1-3]*注数#倍数 1#1013#3*5#10
	elseif butType == '3' then
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		djNum = tonumber(xItem[1])  --直选选基数
		butNum = tonumber(xItem[2])
		if djNum > 3 or djNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if djNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('3', zlStr)
	else
		return 0
	end
	return butNum, butPower, butStr, zlStr

end

function check_kl10( butType, inStr)
	local qxStr;
	local qjStr;
	local qcStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "")
	qxStr = string.gsub( qjStr, "*", "")

	--单式自选任选1#1021#自选1**自选2#倍数
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 10 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."00"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end

	--单式自选组选1#1022#自选1**自选2#倍数
	elseif butType == '2' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		--qcStr = string.gsub(itemList[1],"*","")
		--if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 4 then return 0 end
			butStr = butStr.."01"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--单式自选直选1#1021#自选1**自选2#倍数
	elseif butType == '3' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."02"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--复式任选 	1#1031#任选(1-5)*自选号码#倍数
	elseif butType == '4' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		fjNum = tonumber(xItem[1])       --复式基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if fjNum == 1 then
			if string.len(xItem[2]) < 4 or string.len(xItem[2]) > 40 then return 0 end
		elseif  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 4 then
			if string.len(xItem[2]) < 10 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 5 then
			if string.len(xItem[2]) < 12 or string.len(xItem[2]) > 40 then return 0 end
		else
			return 0
		end

		butStr = butStr.."10"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"
	--复式组选 	1#1032#组选(2/3)*自选号码#倍数
	elseif butType == '5' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		fjNum = tonumber(xItem[1])       --胆脱基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end

		else
			return 0
		end

		butStr = butStr.."11"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"

	--胆拖投注  1#1071#组选[2/3]胆码[1个]*拖码[1-19个]#倍数
	elseif butType == '6' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		djNum = tonumber(xItem[1])       --胆拖基数
		--判断自选号码是否为偶数===========================
		if #xItem ~= 3 then return 0 end
		if string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0 then return 0 end
		--================================================

		if is_str_mix(xItem[2],xItem[3]) then return 0 end


		if  djNum == 2 then
			if string.len(xItem[2]) ~= 2 or (string.len(xItem[2]) + string.len(xItem[3])) < 6 then return 0 end
			butNum = combination(string.len(xItem[3])/2,1)
		elseif djNum == 3 then
			if string.len(xItem[2]) < 2 or string.len(xItem[2]) > 4 or (string.len(xItem[2]) + string.len(xItem[3])) < 8 then return 0 end
			butNum = combination(string.len(xItem[3])/2,(djNum-(string.len(xItem[2])/2)))
		else
			return 0
		end

		butStr = butStr.."21"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."*"..sort_str(xItem[3],20).."^"
	--直选复式投注 1#1041#自选号码第一位(*自选号码第二位*自选号码第三位)#倍数
	elseif butType == '7' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if #xItem < 1 or #xItem >3 then return 0 end

		if #xItem == 2 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 < 3 or string.len(xItem[1])/2 + string.len(xItem[2])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)

			butStr = butStr.."30"..format_power(butPower).."02"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"
		elseif #xItem == 3 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) < 4 or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) or is_str_mix(xItem[1],xItem[3]) or is_str_mix(xItem[2],xItem[3]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)*tonumber(string.len(xItem[3])/2)

			butStr = butStr.."30"..format_power(butPower).."03"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"..format_num(string.len(xItem[3])/2)..sort_str(xItem[3],20).."^"

		else
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[1])/2 < 2 or string.len(xItem[1])/2 > 20  then return 0 end
			butNum = string.len(xItem[1])/2

			butStr = butStr.."30"..format_power(butPower).."01"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"
		end

	else
		return 0
	end

	return butNum,butPower,butStr,inStr
end
--for i=1 ,10000 do
--print(random_kl10('1',"3*2#1234"))
--print(random_kl10('2',"2*2#1234"))
--print(random_kl10('3',"2*2#1234"))
--end
--print(check_kl10('1',"01**0203**020304**01020304**0203040520#9999"))
--print(check_kl10('2',"020304**020304**020304**020304**020320#9999"))
--print(check_kl10('3',"02**020304**0203#9999"))
--print(check_kl10('4',"2*0102030405#10"))
--print(check_kl10('5',"3*0602030405#10"))
--print(check_kl10('6',"3*0303*040506#10"))
--print(check_kl10('7',"0102030405*1020*06070809#1234"))
--分离器
function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}
	while true do
		local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
		if not nFindLastIndex then
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
		break
		end
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
		nFindStartIndex = nFindLastIndex + string.len(szSeparator)
		nSplitIndex = nSplitIndex + 1
	end
	return nSplitArray
end



--随机产生不可重复的数
function sel_dist_num(count,...)
	selected={};
	math.random(0,#arg);
	--math.randomseed(microtime()*1000000);
	math.randomseed(microtime()*1000000);
	if #arg<=count then return arg end
	while #selected < count do
		math.random(#arg);
		table.insert(selected,table.remove(arg,math.random(#arg)));
	end
	return selected;
end



--格式化字符串
function format_num( num )
	num = tonumber(num)
	if num < 10 then
		return "0"..num
	end
	return num;
end

--格式化倍数
function format_power( num )
	num = tonumber(num)
	if num < 10 then
		return "000"..num
	end
	if num < 100 and num >9 then
		return "00"..num
	end
	if num < 1000 and num > 99 then
		return "0"..num
	end
	return num;
end

--数字字符串排序
function sort_str( num , max)
	local out = ""
	local st = {}
	if num == nil then return nil end
	local len = string.len( num )
	if len%2 ~= 0 then
		return nil
	end

	for i=1,len,2 do
		c = string.sub(num,i,i+1)
		if tonumber(c) > max or tonumber(c) <1 then return nil end
		if not in_table( c, st ) then
			table.insert( st, c )
		else
			return nil
		end
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end


--排列组合 m>=n
function permutation( m, n )
	local out=1;
	if m==n then
		for i=1,m do
			out = out*i
		end
	elseif m<n then
		return 0
	else
		for i=m-n+1,m do
			out = out*i
		end
	end

	return out
end

function combination(m, n)
	local out = permutation(m, n)
	local out1 = 1;
	for i=1,n do
		out1 = out1*i
	end
	return (out/out1)
end

--判断字符串是否有相交
function is_str_mix( str1, str2 )
	local t1 = ""
	local len1 = string.len(str1)
	local len2 = string.len(str2)

	for i=1,len1,2 do
		t1 = string.sub(str1,i,i+1)
		for j=1,len2,2 do
			if( t1 == string.sub(str2,j,j+1) ) then return 1 end
		end
	end

	return nil
end

function random_kl10( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选  任选 1#1011#任选[1-5]*注数#倍数 1#1011#1*5#10
	if butType == '1' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		rjNum = tonumber(xItem[1])  --任选基数
		butNum = tonumber(xItem[2])
		if rjNum > 5 or rjNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if rjNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 4 then
			for i=1,butNum do
				local nums = sel_dist_num(4,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 5 then
			for i=1,butNum do
				local nums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('1', zlStr)
	--单式机选组选 1#1012#组选[2/3]*注数#倍数 1#1012#2*5#10
	elseif butType == '2' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		zjNum = tonumber(xItem[1])  --组选基数
		butNum = tonumber(xItem[2])
		if zjNum > 3 or zjNum < 2 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		--二连组
		if zjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		--三前组
		elseif zjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('2', zlStr)

	--单式机选直选 1#1013#直选[1-3]*注数#倍数 1#1013#3*5#10
	elseif butType == '3' then
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		djNum = tonumber(xItem[1])  --直选选基数
		butNum = tonumber(xItem[2])
		if djNum > 3 or djNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if djNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('3', zlStr)
	else
		return 0
	end
	return butNum, butPower, butStr, zlStr

end

function check_kl10( butType, inStr)
	local qxStr;
	local qjStr;
	local qcStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "")
	qxStr = string.gsub( qjStr, "*", "")

	--单式自选任选1#1021#自选1**自选2#倍数
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 10 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."00"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end

	--单式自选组选1#1022#自选1**自选2#倍数
	elseif butType == '2' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		--qcStr = string.gsub(itemList[1],"*","")
		--if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 4 then return 0 end
			butStr = butStr.."01"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--单式自选直选1#1021#自选1**自选2#倍数
	elseif butType == '3' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."02"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--复式任选 	1#1031#任选(1-5)*自选号码#倍数
	elseif butType == '4' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		fjNum = tonumber(xItem[1])       --复式基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if fjNum == 1 then
			if string.len(xItem[2]) < 4 or string.len(xItem[2]) > 40 then return 0 end
		elseif  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 4 then
			if string.len(xItem[2]) < 10 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 5 then
			if string.len(xItem[2]) < 12 or string.len(xItem[2]) > 40 then return 0 end
		else
			return 0
		end

		butStr = butStr.."10"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"
	--复式组选 	1#1032#组选(2/3)*自选号码#倍数
	elseif butType == '5' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		fjNum = tonumber(xItem[1])       --胆脱基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end

		else
			return 0
		end

		butStr = butStr.."11"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"

	--胆拖投注  1#1071#组选[2/3]胆码[1个]*拖码[1-19个]#倍数
	elseif butType == '6' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		djNum = tonumber(xItem[1])       --胆拖基数
		--判断自选号码是否为偶数===========================
		if #xItem ~= 3 then return 0 end
		if string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0 then return 0 end
		--================================================

		if is_str_mix(xItem[2],xItem[3]) then return 0 end


		if  djNum == 2 then
			if string.len(xItem[2]) ~= 2 or (string.len(xItem[2]) + string.len(xItem[3])) < 6 then return 0 end
			butNum = combination(string.len(xItem[3])/2,1)
		elseif djNum == 3 then
			if string.len(xItem[2]) < 2 or string.len(xItem[2]) > 4 or (string.len(xItem[2]) + string.len(xItem[3])) < 8 then return 0 end
			butNum = combination(string.len(xItem[3])/2,(djNum-(string.len(xItem[2])/2)))
		else
			return 0
		end

		butStr = butStr.."21"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."*"..sort_str(xItem[3],20).."^"
	--直选复式投注 1#1041#自选号码第一位(*自选号码第二位*自选号码第三位)#倍数
	elseif butType == '7' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if #xItem < 1 or #xItem >3 then return 0 end

		if #xItem == 2 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 < 3 or string.len(xItem[1])/2 + string.len(xItem[2])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)

			butStr = butStr.."30"..format_power(butPower).."02"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"
		elseif #xItem == 3 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) < 4 or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) or is_str_mix(xItem[1],xItem[3]) or is_str_mix(xItem[2],xItem[3]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)*tonumber(string.len(xItem[3])/2)

			butStr = butStr.."30"..format_power(butPower).."03"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"..format_num(string.len(xItem[3])/2)..sort_str(xItem[3],20).."^"

		else
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[1])/2 < 2 or string.len(xItem[1])/2 > 20  then return 0 end
			butNum = string.len(xItem[1])/2

			butStr = butStr.."30"..format_power(butPower).."01"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"
		end

	else
		return 0
	end

	return butNum,butPower,butStr,inStr
end
--for i=1 ,10000 do
--print(random_kl10('1',"3*2#1234"))
--print(random_kl10('2',"2*2#1234"))
--print(random_kl10('3',"2*2#1234"))
--end
--print(check_kl10('1',"01**0203**020304**01020304**0203040520#9999"))
--print(check_kl10('2',"020304**020304**020304**020304**020320#9999"))
--print(check_kl10('3',"02**020304**0203#9999"))
--print(check_kl10('4',"2*0102030405#10"))
--print(check_kl10('5',"3*0602030405#10"))
--print(check_kl10('6',"3*0303*040506#10"))
--print(check_kl10('7',"0102030405*1020*06070809#1234"))
--分离器
function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}
	while true do
		local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
		if not nFindLastIndex then
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
		break
		end
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
		nFindStartIndex = nFindLastIndex + string.len(szSeparator)
		nSplitIndex = nSplitIndex + 1
	end
	return nSplitArray
end



--随机产生不可重复的数
function sel_dist_num(count,...)
	selected={};
	math.random(0,#arg);
	--math.randomseed(microtime()*1000000);
	math.randomseed(microtime()*1000000);
	if #arg<=count then return arg end
	while #selected < count do
		math.random(#arg);
		table.insert(selected,table.remove(arg,math.random(#arg)));
	end
	return selected;
end



--格式化字符串
function format_num( num )
	num = tonumber(num)
	if num < 10 then
		return "0"..num
	end
	return num;
end

--格式化倍数
function format_power( num )
	num = tonumber(num)
	if num < 10 then
		return "000"..num
	end
	if num < 100 and num >9 then
		return "00"..num
	end
	if num < 1000 and num > 99 then
		return "0"..num
	end
	return num;
end

--数字字符串排序
function sort_str( num , max)
	local out = ""
	local st = {}
	if num == nil then return nil end
	local len = string.len( num )
	if len%2 ~= 0 then
		return nil
	end

	for i=1,len,2 do
		c = string.sub(num,i,i+1)
		if tonumber(c) > max or tonumber(c) <1 then return nil end
		if not in_table( c, st ) then
			table.insert( st, c )
		else
			return nil
		end
	end
	table.sort(st)
	for i=1,table.getn( st ) do
		out = out..st[i]
	end
	return out
end

function in_table ( e, t )
	for _,v in pairs(t) do
		if (v==e) then return true end
	end
	return false
end


--排列组合 m>=n
function permutation( m, n )
	local out=1;
	if m==n then
		for i=1,m do
			out = out*i
		end
	elseif m<n then
		return 0
	else
		for i=m-n+1,m do
			out = out*i
		end
	end

	return out
end

function combination(m, n)
	local out = permutation(m, n)
	local out1 = 1;
	for i=1,n do
		out1 = out1*i
	end
	return (out/out1)
end

--判断字符串是否有相交
function is_str_mix( str1, str2 )
	local t1 = ""
	local len1 = string.len(str1)
	local len2 = string.len(str2)

	for i=1,len1,2 do
		t1 = string.sub(str1,i,i+1)
		for j=1,len2,2 do
			if( t1 == string.sub(str2,j,j+1) ) then return 1 end
		end
	end

	return nil
end

function random_kl10( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选  任选 1#1011#任选[1-5]*注数#倍数 1#1011#1*5#10
	if butType == '1' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		rjNum = tonumber(xItem[1])  --任选基数
		butNum = tonumber(xItem[2])
		if rjNum > 5 or rjNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if rjNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 4 then
			for i=1,butNum do
				local nums = sel_dist_num(4,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif rjNum == 5 then
			for i=1,butNum do
				local nums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('1', zlStr)
	--单式机选组选 1#1012#组选[2/3]*注数#倍数 1#1012#2*5#10
	elseif butType == '2' then

		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		zjNum = tonumber(xItem[1])  --组选基数
		butNum = tonumber(xItem[2])
		if zjNum > 3 or zjNum < 2 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		--二连组
		if zjNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		--三前组
		elseif zjNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('2', zlStr)

	--单式机选直选 1#1013#直选[1-3]*注数#倍数 1#1013#3*5#10
	elseif butType == '3' then
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if string.len(xItem[1]) ~= 1 or string.len(xItem[2]) ~= 1 then return 0 end
		djNum = tonumber(xItem[1])  --直选选基数
		butNum = tonumber(xItem[2])
		if djNum > 3 or djNum < 1 or butNum > 5 or butNum < 1 then return 0 end

		if itemLen > 2 or itemLen < 1 or not butNum  or	string.find(itemList[2],"%D") then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end

		if djNum == 1 then

			for i=1,butNum do
				local nums = sel_dist_num(1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 2 then
			for i=1,butNum do
				local nums = sel_dist_num(2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
				if i<butNum then zlStr = zlStr.."**" end
			end
		elseif djNum == 3 then
			for i=1,butNum do
				local nums = sel_dist_num(3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
				zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
				if i<butNum then zlStr = zlStr.."**" end
			end
		else
			return 0
		end

		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_kl10('3', zlStr)
	else
		return 0
	end
	return butNum, butPower, butStr, zlStr

end

function check_kl10( butType, inStr)
	local qxStr;
	local qjStr;
	local qcStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "")
	qxStr = string.gsub( qjStr, "*", "")

	--单式自选任选1#1021#自选1**自选2#倍数
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 10 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."00"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end

	--单式自选组选1#1022#自选1**自选2#倍数
	elseif butType == '2' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		--qcStr = string.gsub(itemList[1],"*","")
		--if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 4 then return 0 end
			butStr = butStr.."01"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--单式自选直选1#1021#自选1**自选2#倍数
	elseif butType == '3' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		--判断自选号码是否为偶数===========================
		qcStr = string.gsub(itemList[1],"*","")
		if string.len(qcStr)%2 ~= 0 then return 0 end
		--================================================
		for i=1,butNum do
			if string.len(xxItem[i]) > 6 or string.len(xxItem[i]) < 2 then return 0 end
			butStr = butStr.."02"..format_power(butPower)..format_num(string.len(xxItem[i])/2)..sort_str(xxItem[i],20).."^"

		end
	--复式任选 	1#1031#任选(1-5)*自选号码#倍数
	elseif butType == '4' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		fjNum = tonumber(xItem[1])       --复式基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if fjNum == 1 then
			if string.len(xItem[2]) < 4 or string.len(xItem[2]) > 40 then return 0 end
		elseif  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 4 then
			if string.len(xItem[2]) < 10 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 5 then
			if string.len(xItem[2]) < 12 or string.len(xItem[2]) > 40 then return 0 end
		else
			return 0
		end

		butStr = butStr.."10"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"
	--复式组选 	1#1032#组选(2/3)*自选号码#倍数
	elseif butType == '5' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		fjNum = tonumber(xItem[1])       --胆脱基数
		--判断自选号码是否为偶数===========================

		if string.len(xItem[2])%2 ~= 0 then return 0 end
		--================================================
		if #xItem ~= 2 then return 0 end
		if tonumber(xItem[1]) > 5 or tonumber(xItem[1]) < 1 then return 0 end
		butNum = combination(string.len(xItem[2])/2,fjNum)

		if  fjNum == 2 then
			if string.len(xItem[2]) < 6 or string.len(xItem[2]) > 40 then return 0 end
		elseif fjNum == 3 then
			if string.len(xItem[2]) < 8 or string.len(xItem[2]) > 40 then return 0 end

		else
			return 0
		end

		butStr = butStr.."11"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2).."*"..sort_str(xItem[2],20).."^"

	--胆拖投注  1#1071#组选[2/3]胆码[1个]*拖码[1-19个]#倍数
	elseif butType == '6' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")
		djNum = tonumber(xItem[1])       --胆拖基数
		--判断自选号码是否为偶数===========================
		if #xItem ~= 3 then return 0 end
		if string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0 then return 0 end
		--================================================

		if is_str_mix(xItem[2],xItem[3]) then return 0 end


		if  djNum == 2 then
			if string.len(xItem[2]) ~= 2 or (string.len(xItem[2]) + string.len(xItem[3])) < 6 then return 0 end
			butNum = combination(string.len(xItem[3])/2,1)
		elseif djNum == 3 then
			if string.len(xItem[2]) < 2 or string.len(xItem[2]) > 4 or (string.len(xItem[2]) + string.len(xItem[3])) < 8 then return 0 end
			butNum = combination(string.len(xItem[3])/2,(djNum-(string.len(xItem[2])/2)))
		else
			return 0
		end

		butStr = butStr.."21"..format_power(butPower)..format_num(xItem[1])..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."*"..sort_str(xItem[3],20).."^"
	--直选复式投注 1#1041#自选号码第一位(*自选号码第二位*自选号码第三位)#倍数
	elseif butType == '7' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then-- or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >9999 or butPower < 1 then return 0 end
		xStr = string.gsub(itemList[1], "*", "x")
		xItem = Split(xStr, "x")

		if #xItem < 1 or #xItem >3 then return 0 end

		if #xItem == 2 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 < 3 or string.len(xItem[1])/2 + string.len(xItem[2])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)

			butStr = butStr.."30"..format_power(butPower).."02"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"
		elseif #xItem == 3 then
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[2])%2 ~= 0 or string.len(xItem[3])%2 ~= 0  or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) < 4 or (string.len(xItem[1])/2 + string.len(xItem[2])/2 +string.len(xItem[3])/2) > 20 then return 0 end
			if is_str_mix(xItem[1],xItem[2]) or is_str_mix(xItem[1],xItem[3]) or is_str_mix(xItem[2],xItem[3]) then return 0 end
			butNum = tonumber(string.len(xItem[1])/2)*tonumber(string.len(xItem[2])/2)*tonumber(string.len(xItem[3])/2)

			butStr = butStr.."30"..format_power(butPower).."03"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"..format_num(string.len(xItem[2])/2)..sort_str(xItem[2],20).."^"..format_num(string.len(xItem[3])/2)..sort_str(xItem[3],20).."^"

		else
			if string.len(xItem[1])%2 ~= 0 or string.len(xItem[1])/2 < 2 or string.len(xItem[1])/2 > 20  then return 0 end
			butNum = string.len(xItem[1])/2

			butStr = butStr.."30"..format_power(butPower).."01"..format_num(string.len(xItem[1])/2)..sort_str(xItem[1],20).."^"
		end

	else
		return 0
	end

	return butNum,butPower,butStr,inStr
end
--for i=1 ,10000 do
--print(random_kl10('1',"3*2#1234"))
--print(random_kl10('2',"2*2#1234"))
--print(random_kl10('3',"2*2#1234"))
--end
--print(check_kl10('1',"01**0203**020304**01020304**0203040520#9999"))
--print(check_kl10('2',"020304**020304**020304**020304**020320#9999"))
--print(check_kl10('3',"02**020304**0203#9999"))
--print(check_kl10('4',"2*0102030405#10"))
--print(check_kl10('5',"3*0602030405#10"))
--print(check_kl10('6',"3*0303*040506#10"))
--print(check_kl10('7',"0102030405*1020*06070809#1234"))
